package tan.dft.listviewlotrinh;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tan.dft.gps.R;
import tan.dft.gps.Showlotrinh;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Mainlotrinh extends Activity {

	private ListView listLoTrinh;
	private ArrayList<Lotrinh> arraylotrinh;
	private LotrinhAdapter adapter;
	private String myjsonstring, date;
	private Double lat, lng;
	private int idmay, thoigian, idmayql, idmayintent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainlotrinh);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		idmayintent = (Integer) bundle.getInt("idmay");

		listLoTrinh = (ListView) findViewById(R.id.listlotrinh);

		arraylotrinh = new ArrayList<Lotrinh>();
		createArray();
		adapter = new LotrinhAdapter(Mainlotrinh.this, R.layout.lotrinhitem,
				arraylotrinh);
		listLoTrinh.setAdapter(adapter);

		listLoTrinh.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				int k = arraylotrinh.get(arg2).getIdmay();

				Intent intent = new Intent().setClass(getBaseContext(),
						Showlotrinh.class);

				ArrayList<Lotrinh> addyExtras = new ArrayList<Lotrinh>();

				for (int i = 0; i < arraylotrinh.size(); i++) {

					int l = arraylotrinh.get(i).getIdmay();

					if (k == l) {
						addyExtras.add(arraylotrinh.get(i));
					}

				}

				intent.putParcelableArrayListExtra("mylist", addyExtras);

				startActivity(intent);

			}
		});
	}

	private void createArray() {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(getAssets().open(
					"lotrinh.txt")));
			String temp;
			while ((temp = br.readLine()) != null)
				sb.append(temp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		myjsonstring = sb.toString();
		try {
			JSONObject jsonObjMain = new JSONObject(myjsonstring);
			JSONArray jsonArray = jsonObjMain.getJSONArray("lotrinhmay");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObj = jsonArray.getJSONObject(i);
				idmayql = jsonObj.getInt("idmayql");
				if (idmayql == idmayintent) {
					idmay = jsonObj.getInt("idmay");
					JSONArray jsonArrayql = jsonObj.getJSONArray("lotrinh");
					for (int j = 0; j < jsonArrayql.length(); j++) {
						JSONObject jsonObjectql = jsonArrayql.getJSONObject(j);
						date = jsonObjectql.getString("date");
						JSONObject jObj = jsonObjectql.getJSONObject("toado");
						lat = jObj.getDouble("lat");
						lng = jObj.getDouble("lng");
						thoigian = jsonObjectql.getInt("thoigian");

						Lotrinh lotrinh = new Lotrinh(idmayql, idmay, date,
								lat, lng, thoigian);
						arraylotrinh.add(lotrinh);
					}

				}

			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

}
